package com.akk.module.avs.service;

import com.akk.module.avs.entity.AvsLecturer;
import com.akk.module.avs.entity.AvsLecturerExample;
import com.akk.module.avs.mapper.AvsLecturerMapper;
import com.akk.utils.lecturer.CompositeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AvsLectureService {

    @Autowired
    private AvsLecturerMapper lecturerMapper;

    @Autowired
    private AvsLecturerExample lecturerExample;

    @Autowired
    private CompositeImage compositeImage;

    public AvsLecturer queryByCertificateNumberAndType(String value, int type){
        lecturerExample.clear();
        AvsLecturer entity;
        if (type == 1){
            lecturerExample.createCriteria().andCertificateNumberEqualTo(value);
        }
        else {
            lecturerExample.createCriteria().andIdCardEqualTo(value);
        }
        List<AvsLecturer> list = lecturerMapper.selectByExample(lecturerExample);
        if (list.size() == 1)
            entity = list.get(0);
        else
            return null;
        if (entity.getFileUrl() == null || entity.getFileUrl().length()<1){
            String fileUrl = compositeImage.certificate(entity);
            if (fileUrl == null)
                return null;
            entity.setFileUrl(fileUrl);
            update(entity);
        }
        return entity;
    }

    public void update(AvsLecturer lecturer){
        lecturerMapper.updateByPrimaryKey(lecturer);
    }


    public AvsLecturer queryById(Long id) {
        return lecturerMapper.selectByPrimaryKey(id);
    }

    public void save(AvsLecturer entity) {
        lecturerMapper.insert(entity);
    }

    public void del(Long id) {
        lecturerMapper.deleteByPrimaryKey(id);
    }

    public List<AvsLecturer> queryByParams(Map params) {
        lecturerExample.clear();
        AvsLecturerExample.Criteria criteria = lecturerExample.createCriteria();
        if (params.containsKey("createDate")){

        }
        return lecturerMapper.selectByExample(lecturerExample);
    }
}
